package com.qzsoft.zhjd.machine.zhgcloud.fuel;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.zhjd.machine.modules.machinefuel.entity.MachineFuelEntity;
import com.qzsoft.zhjd.machine.modules.machinefuel.service.MachineFuelService;
import com.qzsoft.zhjd.machine.zhgcloud.ZhgHandler;
import com.qzsoft.zhjd.machine.zhgcloud.ZhgResultDataCallback;
import com.qzsoft.zhjd.machine.zhgcloud.fuel.model.ZhgFuelModel;
import com.qzsoft.zhjd.machine.zhgcloud.worktime.model.ZhgFuelConsumeModel;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName ZhgFuelHandler
 * @Description 设备油位处理
 * @author linhuawei
 * @since 2021-11-04
 */
@Slf4j
@Component
public class ZhgFuelHandler {

    @Value("${zhgcloud.urls.fuel}")
    private String fuelUrl;

    @Value("${zhgcloud.urls.fuelConsume}")
    private String fuelConsumeUrl;

    @Autowired
    private ZhgHandler zhgHandler;

    @Autowired
    private MachineFuelService machineFuelService;

    /**
     * @Title fuel
     * @param machineKeyMap 设备编号主键映射
     * @Description 获取设备实时油位信息
     * @return 转化后的油位信息
     */
    public List<MachineFuelEntity> fuel(Map<String, String> machineKeyMap) {
        // 获取数据列表
        List<ZhgFuelModel> collectList = getFuelList();
        if (ObjectUtils.isEmpty(collectList)) {
            return null;
        }
        
        Date date = new Date();

        // 转化采集数据
        List<MachineFuelEntity> list = new ArrayList<MachineFuelEntity>();
        MachineFuelEntity entity = null;
        ZhgFuelModel model = null;
        String machineId = null;
        for (int i = 0, len = collectList.size(); i < len; i++) {
            // 如果数据无效，不做处理
            model = collectList.get(i);
            if (!model.isValid()) {
                log.info("无效={}", model);
                continue;
            }

            // 获取设备主键
            machineId = machineKeyMap.get(model.getMachine_key());
            if (ObjectUtils.isEmpty(machineId)) {
                continue;
            }

            // 加油记录数据对象转化
            entity = new MachineFuelEntity();
            entity.setMachineId(machineId);
            entity.setMachineKey(model.getMachine_key());
            entity.setReportDate(model.getLast_report_at());
            entity.setVolume(model.getVolume());
            entity.setPercentage(model.getPercentage());
            entity.setCreateDate(date);
            list.add(entity);
        }

        // 保存实时油位
        if (ObjectUtils.isNotEmpty(list)) {
            machineFuelService.saveBatch(list, 100);
        }
        return list;
    }

    /**
     * @Title getFuelList
     * @Description 获取油位数据
     * @return 油位数据
     */
    public List<ZhgFuelModel> getFuelList() {
        // 查询参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("count_per_page", 0);

        // 获取查询结果
        ZhgResultDataCallback callback = (JSONObject resultJson) -> {
            return resultJson.getString("data_list");
        };
        return zhgHandler.httpGetList(fuelUrl, params, ZhgFuelModel.class, callback);
    }

    /**
     * @Title getfuelConsumeList
     * @Description 获取油耗列表
     * @param reportDate 上报日期
     * @return 油耗列表
     */
    public List<ZhgFuelConsumeModel> getfuelConsumeList(String reportDate) {
        // 设置查询条件
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("date", reportDate);

        // 获取油耗信息
        ZhgResultDataCallback callback = (JSONObject resultJson) -> {
            return null == resultJson.getJSONObject("result") ? null
                    : resultJson.getJSONObject("result").getString("data_list");
        };
        return zhgHandler.httpGetList(fuelConsumeUrl, params, ZhgFuelConsumeModel.class, callback);
    }

}
